
// #include "util.hpp"
// #include "level.hpp"
// #include "message.hpp"
// #include "format.hpp"
// #include "out.hpp"
// #include "logger.hpp"
// #include "buffer.hpp"
// #include <unistd.h>
#include "lcllog.h"
using namespace std;
void test_util()
{
    cout << lcllog::util::Date::now() << endl;
    string pathname = "abc/bcd/de/lcl.txt";
    cout << lcllog::util::file::exists(pathname) << endl;
    lcllog::util::file::create_directory(lcllog::util::file::path(pathname));
    cout << lcllog::util::file::exists(pathname) << endl;
}

void test_level()
{
    cout << lcllog::LogLevel::tostring(lcllog::LogLevel::value::DEBUG) << endl;
    cout << lcllog::LogLevel::tostring(lcllog::LogLevel::value::FATAL) << endl;
    cout << lcllog::LogLevel::tostring(lcllog::LogLevel::value::WARN) << endl;
}

void test_format()
{
    lcllog::LogMsg msg("root", "main.c", 12, lcllog::LogLevel::value::FATAL, "格式化测试！");
    lcllog::Formatter fmt;
    // lcllog::Formatter fmt("abc%%abc[%d{%H:%M:%S}] %m%n");
    // lcllog::Formatter fmt("abc%%abc[%d{%H:%M:%S] %m%n");
    // lcllog::Formatter fmt("abc%%abc[%d{%H:%M:%S}] %m%");
    // lcllog::Formatter fmt("abc%%abc%g%g[%d{%H:%M:%S}] %m%n");
    std::string str = fmt.format(msg);
    cout << str << endl;
}

void test_out()
{
    lcllog::LogMsg msg("root", "main.c", 12, lcllog::LogLevel::value::DEBUG, "格式化测试！");
    lcllog::Formatter fmt;
    std::string str = fmt.format(msg);
    lcllog::LogOut::ptr std_out = lcllog::OutFactory::create<lcllog::StdoutOut>();
    lcllog::LogOut::ptr file_out = lcllog::OutFactory::create<lcllog::FileOut>("./logfile/test.log");
    lcllog::LogOut::ptr roll_out = lcllog::OutFactory::create<lcllog::RollOut>("./logfile/roll-", 1024*1024);
    std_out->log(str.c_str(), str.size());
    file_out->log(str.c_str(), str.size());
    size_t cursize = 0;
    size_t count = 0;
    while(cursize < 1024 * 1024 * 10)
    {
        std::string tmp = str + std::to_string(count++);
        roll_out->log(tmp.c_str(), tmp.size());
        cursize += tmp.size();
    }
}

void test_RollByTimeOut()
{
    lcllog::LogMsg msg("root", "main.c", 12, lcllog::LogLevel::value::DEBUG, "格式化测试！");
    lcllog::Formatter fmt;
    std::string str = fmt.format(msg);
    lcllog::LogOut::ptr time_out = lcllog::OutFactory::create<RollByTimeOut>("./logfile/roll-", TimeGap::GAP_MINUTE);
    time_t old = lcllog::util::Date::now();
    while(lcllog::util::Date::now() < old + 120)
    {   
        time_out->log(str.c_str(), str.size());
    }
}

void test_logger()
{
    std::string logger_name ="sync_logger";
    lcllog::LogLevel::value limit = lcllog::LogLevel::value::WARN;
    lcllog::Formatter::ptr fmt(new lcllog::Formatter());
    lcllog::LogOut::ptr std_out = lcllog::OutFactory::create<lcllog::StdoutOut>();
    lcllog::LogOut::ptr file_out = lcllog::OutFactory::create<lcllog::FileOut>("./logfile/test.log");
    lcllog::LogOut::ptr roll_out = lcllog::OutFactory::create<lcllog::RollOut>("./logfile/roll-", 1024*1024);
    std::vector<lcllog::LogOut::ptr> outs = {std_out, file_out, roll_out};
    // 这里构造日志器之前需要构造很多的必须部件，可以使用建造者模式来简化。
    lcllog::Logger::ptr logger(new lcllog::SyncLogger(logger_name, fmt, outs, limit));

    logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    logger->info(__FILE__, __LINE__, "%s", "测试日志");
    logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    logger->error(__FILE__, __LINE__, "%s", "测试日志");
    logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

    size_t cursize = 0;
    size_t count = 0;
    while(cursize < 1024 * 1024 * 10)
    {
        logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
        cursize += 50;
    }
}

void test_logger_builder()
{
    // std::string logger_name ="sync_logger";
    // lcllog::LogLevel::value limit = lcllog::LogLevel::value::WARN;
    // lcllog::Formatter::ptr fmt(new lcllog::Formatter());
    // lcllog::LogOut::ptr std_out = lcllog::OutFactory::create<lcllog::StdoutOut>();
    // lcllog::LogOut::ptr file_out = lcllog::OutFactory::create<lcllog::FileOut>("./logfile/test.log");
    // lcllog::LogOut::ptr roll_out = lcllog::OutFactory::create<lcllog::RollOut>("./logfile/roll-", 1024*1024);
    // std::vector<lcllog::LogOut::ptr> outs = {std_out, file_out, roll_out};
    // // 这里构造日志器之前需要构造很多的必须部件，可以使用建造者模式来简化。
    // lcllog::Logger::ptr logger(new lcllog::SyncLogger(logger_name, fmt, outs, limit));

    std::unique_ptr<lcllog::LoggerBuilder> builder(new lcllog::LocalLoggerBuilder());
    // 可以增加一个指挥者来进行这些操作，但是指挥者的作用一般是需要特定的构造顺序时才要，这里无所谓哪一个先构造，所以不要也行
    builder->buildLoggerName("sync_logger");
    builder->buildLoggerLimitLevel(lcllog::LogLevel::value::WARN);
    builder->buildLoggerType(lcllog::LoggerType::LOGGER_SYNC);
    builder->buildFormatter("%m%n");
    builder->buildOut<lcllog::FileOut>("./logfile/test.log");
    builder->buildOut<lcllog::StdoutOut>();

    lcllog::Logger::ptr logger = builder->build();

    logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    logger->info(__FILE__, __LINE__, "%s", "测试日志");
    logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    logger->error(__FILE__, __LINE__, "%s", "测试日志");
    logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

    size_t cursize = 0;
    size_t count = 0;
    while(cursize < 1024 * 1024 * 10)
    {
        logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
        cursize += 50;
    }
}

void test_buffer()
{
    std::ifstream ifs("./logfile/test.log", std::ios::binary);
    if(ifs.is_open() == false) return;
    ifs.seekg(0, std::ios::end); // 读位置跳转到文件末尾
    size_t fsize = ifs.tellg();  // 获取当前读位置相对于起始位置的偏移量
    ifs.seekg(0, std::ios::beg); // 重新跳转到起始位置
    std::string body;
    body.resize(fsize);
    ifs.read(&body[0], fsize); // 从ifs流中提取fsize个字符，放入body中，起始位置为body[0]
    if(ifs.good() == false) return;
    ifs.close();


    lcllog::Buffer buffer;
    for(int i = 0; i< body.size(); i++)
    {
        buffer.push(&body[i],1);
    }
    std::ofstream ofs("./logfile/tmp.log",std::ios::binary);
    if(ofs.is_open() == false) return;
    //ofs.write(buffer.begin(),buffer.readAblesize());
    size_t rsize = buffer.readAbleSize();
    for(int i = 0; i < rsize; i++)
    {
        ofs.write(buffer.begin(), 1); // 从buffer中读取1字符内容写入流ofs中。
        if(ofs.good()== false) return;
        buffer.pop(1);
    }
    ofs.close();
}

void test_AsyncLogger()
{
    std::unique_ptr<lcllog::LoggerBuilder> builder(new lcllog::LocalLoggerBuilder());
    // 可以增加一个指挥者来进行这些操作，但是指挥者的作用一般是需要特定的构造顺序时才要，这里无所谓哪一个先构造，所以不要也行
    builder->buildLoggerName("async_logger");
    builder->buildLoggerLimitLevel(lcllog::LogLevel::value::WARN);
    builder->buildLoggerType(lcllog::LoggerType::LOGGER_ASYNC);
    builder->buildFormatter("[%c]%m%n");
    builder->buildEnUnsafeAsync();
    builder->buildOut<lcllog::FileOut>("./logfile/testAsync.log");
    builder->buildOut<lcllog::StdoutOut>();

    lcllog::Logger::ptr logger = builder->build();

    logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    logger->info(__FILE__, __LINE__, "%s", "测试日志");
    logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    logger->error(__FILE__, __LINE__, "%s", "测试日志");
    logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

    size_t count = 0;
    while(count < 500000)
    {
        logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
    }
}

void test_log()
{
    // 优化，lcllog::LoggerManager::getInstance().getLogger("async_logger");
    lcllog::Logger::ptr logger = lcllog::LoggerManager::getInstance().getLogger("root");
    // 这里需要用户自己输入文件名和行号，增加了使用者难度
    // 优化
    // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    // logger->info(__FILE__, __LINE__, "%s", "测试日志");
    // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    // logger->error(__FILE__, __LINE__, "%s", "测试日志");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

    logger->error("%s", "测试日志");
    logger->fatal( "%s", "测试日志");

    size_t count = 0;
    while(count < 500000)
    {
        logger->fatal(__FILE__, __LINE__, "测试日志-%d", count++);
    }
}

void test_lcllog()
{
    // lcllog::Logger::ptr logger = lcllog::getLogger("async_logger");
    lcllog::Logger::ptr logger = lcllog::getLogger("sync_logger");
    // logger->debug("%s", "测试日志");
    // logger->info("%s", "测试日志");
    // logger->warn("%s", "测试日志");
    // logger->error("%s", "测试日志");
    // logger->fatal("%s", "测试日志");
    LOG_DEBUG(logger, "%s", "测试日志!!!");
    LOG_INFO(logger, "%s", "测试日志!!!");
    LOG_WARN(logger, "%s", "测试日志!!!");
    LOG_ERROR(logger, "%s", "测试日志!!!");
    LOG_FATAL(logger, "%s", "测试日志!!!");

    // 调用默认的日志器
    // DEBUG("%s", "测试日志");
    // INFO("%s", "测试日志");
    // WARN("%s", "测试日志");
    // ERROR("%s", "测试日志");
    // FATAL("%s", "测试日志");

    // size_t count = 0;
    // while(count < 500000)
    // {
    //     FATAL("测试日志-%d", count++);
    // }
}
int main()
{
    // test_util();
    // test_level();
    // test_format();
    // test_out();
    // test_RollByTimeOut();
    // test_logger();
    // test_logger_builder();
    // test_buffer();
    // test_AsyncLogger();

    std::unique_ptr<lcllog::LoggerBuilder> builder(new lcllog::GlobalLoggerBuilder());
    builder->buildLoggerName("sync_logger");
    builder->buildLoggerLimitLevel(lcllog::LogLevel::value::WARN);
    builder->buildLoggerType(lcllog::LoggerType::LOGGER_SYNC);
    builder->buildFormatter("[%c][%L][%f:%l]%T%m%n");
    builder->buildOut<lcllog::FileOut>("./logfile/testAsync.log");
    builder->buildOut<lcllog::StdoutOut>();
    builder->build();

    // test_log();
    test_lcllog();
    return 0;
}